﻿using Parylene_CVD.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Parylene_CVD.Tools
{
    /// <summary>
    /// 操作全局变量字典工具类
    /// </summary>
    public class DeviceRealDataTools
    {
        /// <summary>
        /// 根据设备名和实时数据为全局变量字典中添加数据
        /// </summary>
        /// <param name="deviceName">设备名</param>
        /// <param name="data">实时数据</param>
        public static void AddDeviceData(string deviceName, RealTimeData data)
        {
            if (string.IsNullOrEmpty(deviceName) || data == null)
            {
                throw new ArgumentException("设备名和实时数据不能为空");
            }

            GlobalVariables.DeviceRealTimeData.TryAdd(deviceName, data);
        }
        /// <summary>
        /// 根据设备名删除全局变量中的对应数据
        /// </summary>
        /// <param name="deviceName">设备名</param>
        public static void RemoveDeviceData(string deviceName) {
            if (string.IsNullOrEmpty(deviceName))
            {
                throw new ArgumentException("设备名不能为空");
            }

            GlobalVariables.DeviceRealTimeData.TryRemove(deviceName, out var removedData);
        }
        /// <summary>
        /// 根据设备名和新的实时数据更新全局变量中的数据
        /// </summary>
        /// <param name="deviceName">设备名</param>
        /// <param name="newData">新的实时数据</param>
        public static void UpdateDeviceData(string deviceName, RealTimeData newData)
        {
            if (string.IsNullOrEmpty(deviceName) || newData == null)
            {
                throw new ArgumentException("设备名和新的实时数据不能为空");
            }

            GlobalVariables.DeviceRealTimeData.AddOrUpdate(
                deviceName,
                newData, // 添加时的值
                (key, existingValue) =>
                {
                    // 更新时直接替换为新的 RealTimeData
                    return newData;
                }
            );
        }
        /// <summary>
        /// 根据设备名获取该设备当前的实时数据
        /// </summary>
        /// <param name="deviceName">设备名</param>
        /// <returns>实时数据</returns>
        public static RealTimeData GetDeviceData(string deviceName)
        {
            if (string.IsNullOrEmpty(deviceName))
            {
                throw new ArgumentException("设备名不能为空");
            }

            // 通过字典获取设备数据，返回 null 如果没有找到
            GlobalVariables.DeviceRealTimeData.TryGetValue(deviceName, out var realTimeData);
            return realTimeData; // 如果设备不存在，返回 null
        }

    }
}
